package com.example.video.video_server.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.video.common.Constant;
import com.example.video.common.config.RabbitMQ.Common;
import com.example.video.common.config.RabbitMQ.VideoTranscode;
import com.example.video.common.entity.FileResourceInfo;
import com.example.video.common.exception.ServiceException;
import com.example.video.common.util.ResponseWrap;
import com.example.video.file_server.common.FFmpeg.domain.VideoMetaInfo;
import com.example.video.file_server.controller.FileController;
import com.example.video.video_server.entity.VideoDramasSeries;
import com.example.video.video_server.entity.VideoResource;
import com.example.video.video_server.mapper.VideoResourceMapper;
import com.example.video.video_server.service.IVideoResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 视频资源表 服务实现类
 * </p>
 *
 * @author yj
 */
@Service
public class VideoResourceServiceImpl extends ServiceImpl<VideoResourceMapper, VideoResource> implements IVideoResourceService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private FileController fileController;

    @Override
    @Transactional
    public boolean del(Integer id) {
        VideoResource byId = this.getById(id);
        if (byId == null){
            return true;
        }
        this.removeById(id);
        String url = byId.getUrl();
        if (!StringUtils.isBlank(url)){
            fileController.delete(byId.getBucketName(),byId.getObjectName());
        }
        return false;
    }

    @Override
    @Transactional
    public void transcode(Integer id) {
        VideoResource byId = this.getById(id);
        if (byId == null || !byId.getStatus().equals(Constant.VideoSourceStatus.WAITE_TRANSCODE.value)){
            throw new ServiceException("该资源不存在或已转码");
        }
        byId.setStatus(Constant.VideoSourceStatus.TRANSCODING.value);
        this.updateById(byId);
        FileResourceInfo fileResourceInfo = new FileResourceInfo().setUrl(byId.getUrl()).setRelationId(byId.getId()).setContentType(byId.getContentType());
        amqpTemplate.convertAndSend(Common.EXCHANGE, VideoTranscode.REQUEST_KEY, JSON.toJSONString(fileResourceInfo));
    }

    @Override
    public boolean addTx(List<VideoResource> videoResources) {
        for (VideoResource videoResource : videoResources){
            ResponseWrap<VideoMetaInfo> responseWrap = fileController.uploadByM3u8Url(videoResource.getUrl());
            VideoMetaInfo data = responseWrap.getData();
            videoResource.setUrl(data.getUrl());
            videoResource.setBucketName(data.getBucketName());
            videoResource.setObjectName(data.getObjectName());
            videoResource.setStatus("2");
            videoResource.setContentType("m3u8");
        }
        boolean b = this.saveBatch(videoResources);
        return false;
    }
}
